﻿using System.Collections;
using MyUtil;
using Unity.Netcode;
using UnityEngine;

public class EnemyManager : NetworkBehaviour
{
    public static EnemyManager Instance;    
    [Header("Prefab")]
    [SerializeField]
    GameObject greenSlimePrefab,
        purpleSlimePrefab,
        birthIdentifier;

    [Header("Common")]
    [SerializeField] int produceNum = 5;
    [SerializeField] Transform spawnTF;
    
    [Header("Timer")]
    [SerializeField] float produceCd=2;
    Timer produceTimer;
    
    bool isFirst = true;

    private void Awake()
    {                       
        if(Instance == null)
            Instance = this;
        else if(Instance!=this)
        {
            Destroy(gameObject);
        }        
    }
    
    public override void OnNetworkSpawn()
    {
        if (!IsServer)
            return;
        
        produceTimer = TimerManager.Instance.CreateTimer(produceCd, OnProduceTimerTimeout, false);                
    }

    public override void OnNetworkDespawn()
    {                
        if (TimerManager.Instance != null && IsServer)
            TimerManager.Instance.RemoveTimer(produceTimer);
    }

    //private void Update()
    //{
    //    if (!IsServer)
    //        return;
    //    if (Input.GetKeyDown(KeyCode.C))
    //    {
    //        StopWork();
    //    }
    //    if (Input.GetKeyDown(KeyCode.V))
    //    {
    //        produceTimer.Pause();
    //    }
    //}

    public void StartWork()
    {        
        produceTimer.Start();
        if (isFirst)
        {
            produceTimer.PassTime = 1f;
            isFirst = false;
        }
    }

    public void StopWork()
    {
        produceTimer.Pause();
        StopAllCoroutines();        
        OnDestroyAllEnemy();        
    }

    void OnDestroyAllEnemy()
    {
        for (int i = spawnTF.childCount - 1; i >= 0; i--)
        {
            var e = spawnTF.GetChild(i);
            if (e.gameObject.activeInHierarchy && e.TryGetComponent<NetworkObject>(out NetworkObject networkObject) && networkObject.IsSpawned)
            {
                networkObject.Despawn();
            }
        }
    }

    IEnumerator GenerateSlime(NetworkObject bi, int type, Vector2 pos)
    {        
        yield return new WaitForSeconds(1);
        bi.Despawn();
        Slime slime;
        if (type == 1)
        {
            slime = Instantiate(greenSlimePrefab,pos,Quaternion.identity).GetComponent<Slime>();
        }
        else
        {
            slime = Instantiate(purpleSlimePrefab,pos,Quaternion.identity).GetComponent<Slime>();
        }        
        slime.NetworkObject.Spawn(true);
        slime.NetworkObject.TrySetParent(spawnTF);        
    }

    void OnProduceTimerTimeout()
    {        
        for (int i = 0; i < produceNum; i++)
        {            
            Vector2 pos = UnityEngine.Random.insideUnitCircle * 7;
            int type = Random.Range(0, 2);
            var bi = Instantiate(birthIdentifier);
            bi.transform.position = pos;
            var biNetObj=bi.GetComponent<NetworkObject>();            
            biNetObj.Spawn(true);
            biNetObj.TrySetParent(spawnTF);
            StartCoroutine(GenerateSlime(biNetObj,type,pos));
        }
    }
}
